//
//  vdShape.m
//  Void Dead
//
//  Created by Sidney Just on 17.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdShape.h"
#import "vdMisc.h"


@implementation vdShape


#pragma mark -
#pragma mark Rendering

- (void)draw:(NSTimeInterval)deltaTime {
	
	if(rotation == 0) 
	{
		vertices[0] = aPos_x;
		vertices[1] = mother.backingHeight - aPos_y - (size_y * scale_y);
		
		vertices[2] = aPos_x + (size_x * scale_x);
		vertices[3] = mother.backingHeight - aPos_y - (size_y * scale_y);
		
		vertices[4] = aPos_x;
		vertices[5] = mother.backingHeight - aPos_y;
		
		vertices[6] = aPos_x + (size_x * scale_x);
		vertices[7] = mother.backingHeight - aPos_y;
		
		vertices[8] = aPos_x + (size_x * scale_x);
		vertices[9] = mother.backingHeight - aPos_y - (size_y * scale_y);
		
		vertices[10] = aPos_x;
		vertices[11] = mother.backingHeight - aPos_y;
	} else {
		float vert_temppos_x;
		float vert_temppos_y;
		
		float vert_dist;
		float vert_ang;
			
		float temp = rotation;
		rotation = degreesToRadian(rotation);
		
		vert_temppos_x = (-size_x * scale_x) * 0.5f;
		vert_temppos_y = (-size_y * scale_y) * 0.5f;
		vert_dist = sqrt(vert_temppos_x * vert_temppos_x + vert_temppos_y * vert_temppos_y);
		vert_ang =  atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[0] = vert_temppos_x;
		vertices[1] = vert_temppos_y;
		
		vert_temppos_x =  (size_x * scale_x) * 0.5f;
		vert_temppos_y = (-size_y * scale_y) * 0.5f;
		vert_ang = atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[2] = vert_temppos_x;
		vertices[3] = vert_temppos_y;
		
		
		vert_temppos_x = (-size_x * scale_x) * 0.5f;
		vert_temppos_y =  (size_y * scale_y) * 0.5f;
		vert_ang = atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[4] = vert_temppos_x;
		vertices[5] = vert_temppos_y;
		
		
		vert_temppos_x = (size_x * scale_x) * 0.5f;
		vert_temppos_y = (size_y * scale_y) * 0.5f;
		vert_ang = atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[6] = vert_temppos_x;
		vertices[7] = vert_temppos_y;
		
		
		vert_temppos_x =  (size_x * scale_x) * 0.5f;
		vert_temppos_y = (-size_y * scale_y) * 0.5f;
		vert_ang = atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[8] = vert_temppos_x;
		vertices[9] = vert_temppos_y;
		
		
		vert_temppos_x = (-size_x * scale_x) * 0.5f;
		vert_temppos_y =  (size_y * scale_y) * 0.5f;
		vert_ang = atan2(vert_temppos_y, vert_temppos_x);
		vert_temppos_x = cos(vert_ang + rotation) * vert_dist + aPos_x + (size_x * scale_x) * 0.5f;
		vert_temppos_y = sin(vert_ang + rotation) * vert_dist + mother.backingHeight - aPos_y - (size_y * scale_y) * 0.5f;
		
		vertices[10] = vert_temppos_x;
		vertices[11] = vert_temppos_y; 
		
		rotation = temp;
	}
	
	[[vdRenderer sharedRenderer] setMode:GL_TRIANGLES];
	if(!modelMatrix)
	{
		[[vdRenderer sharedRenderer] addVertices:vertices texCoords:texCoords posPerVertex:2 size:12 texture:texture color:color];
	} else {
		[[vdRenderer sharedRenderer] addVertices:vertices texCoords:texCoords posPerVertex:2 size:12 texture:texture color:color andMatrix:modelMatrix];
	}

}

- (void)setTexture:(vdTexNode *)tex {
	if(tex)
	{
		size_x = tex.dimension_x;
		size_y = tex.dimension_y;
		
		texCoords[0] = 0;
		texCoords[1] = 1;
		
		texCoords[2] = 1; 
		texCoords[3] = 1;
		
		texCoords[4] = 0; 
		texCoords[5] = 0;
		
		texCoords[6] = 1; 
		texCoords[7] = 0;
		
		texCoords[8] = 1; 
		texCoords[9] = 1;
		
		texCoords[10] = 0; 
		texCoords[11] = 0;
	}
	
	texture = tex;
}

#pragma mark -
#pragma mark Con- Destructor

+ (vdShape *)shapeWithTexture:(vdTexNode *)tex {
	return [[vdShape alloc] initWithTexture:tex];
}

- (id)initWithTexture:(vdTexNode *)tex {
	if(self = [super init])
	{
		[self setTexture:tex];
	}
	return self;
}

- (id)init {
	if(self = [super init])
	{
		[self setTexture:NULL];
	}
	return self;
}

- (void)dealloc {
	[super dealloc];
}

@end
